﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EntityFramework;
using System.Text;
using System.Data;
using BO;

namespace ProjectQuaDep
{
    public partial class SearchProduct : System.Web.UI.Page
    {
        public int ZoneFocus = 1;

        private const string ProductFocusItemFormat = @"<div class=""item {0} mgb10"">
                    {1}
                    <div class=""name"">
                        {2}</div>
                    <div class=""img"">
                        <a href=""{4}"" title=""{3}"">
                            <img alt=""{3}"" src=""{5}"" /></a>
                    </div>
                    <div class=""code-name"">
                        {6}</div>
                    <div class=""desc"">
                        {7}</div>
                    <div class=""shopping"">
                        <div class=""price"">
                            <div class=""current"">
                                {8}</div>
                            <div class=""discount"">
                                {9}</div>
                        </div>
                        <a href=""javascript:void(0);"" class=""cart""></a>
                    </div>
                    <div class=""chitietshow"">
                        <a href=""{4}"" title=""{3}"">Chi tiết »</a>
                    </div>
                </div>";
        private const string ProductNewFormat = "<i class=\"new\"></i>";
        int PageSize = 9;
        int PageIndex = lib.PageIndex;
        int ZoneID = lib.ZoneID;
        string Search = lib.KeySearch;
        int CompanyID = lib.CompanyID;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable dt = BO.ProductUserBO.ProductUBO.GetListProductByZoneBO(PageSize.ToString(), PageIndex.ToString(), ZoneID.ToString(), Search, CompanyID.ToString());
                // ProductsBO.GetTopProductIsFocus(3, ZoneFocus);
                if (dt.Rows.Count != 0)
                {
                    //ltrFocus.Text = ZoneProductBO.GetZoneProductNameById(ZoneFocus);
                    var sb = new StringBuilder();
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        string cls = (i + 1) % 3 == 0 ? "mr0" : string.Empty;
                        long date = 0;
                        long.TryParse(dt.Rows[i][Const.tbProducts.EndDateNew].ToString(), out date);
                        string pNew = date > 0 && date >= long.Parse(DateTime.Now.ToString("yyyyMMdd")) ? ProductNewFormat : string.Empty;

                        string price = int.Parse(dt.Rows[i][Const.tbProducts.TypeSale].ToString()) == 1 ? "Liên hệ" : Utils.ConvertMoneyNice(dt.Rows[i][Const.tbProducts.Price].ToString(), BO.Const.FormatMoney);
                        string sale = int.Parse(dt.Rows[i][Const.tbProducts.TypeSale].ToString()) == 1 ? string.Empty : Utils.ConvertMoneyNice(dt.Rows[i][Const.tbProducts.Sale].ToString(), BO.Const.FormatMoney);
                        if (int.Parse(dt.Rows[i][Const.tbProducts.Sale].ToString()) == 0)
                        {
                            sale = "";
                        }
                        sb.AppendFormat(ProductFocusItemFormat, cls
                            , pNew
                            , dt.Rows[i][Const.tbProducts.Title].ToString()
                            , HttpUtility.HtmlEncode(dt.Rows[i][Const.tbProducts.Title].ToString())
                            , dt.Rows[i][Const.tbProducts.Url].ToString()
                            , BO.Utils.CropImage(172, 140, BO.Utils.GetOneImagesByArr(dt.Rows[i][Const.tbProducts.Avatar].ToString()))
                            , dt.Rows[i][Const.tbProducts.ProductCode].ToString()
                            ,  BO.Utils.getSubStringNice(dt.Rows[i][Const.tbProducts.Sapo].ToString(),50)
                            , price
                            , sale);
                    }
                    ltrListFocus.Text = sb.ToString();
                    //set phan trang
                    int totalRows = int.Parse(BO.ProductUserBO.ProductUBO.CountGetListProductByZoneBO(ZoneID.ToString(), Search, CompanyID.ToString()).Rows[0][Const.tbProducts.TongBanGhi].ToString());
                    ltCountRows.Text = ": có " + totalRows + " sản phẩm "+ Search + " được tìm thấy";
                    PaddingSearch1.PageSize = PageSize;
                    int SumPage = totalRows % PageSize == 0 ? totalRows / PageSize : (totalRows / PageSize)+1;
                    PaddingSearch1.SumPage = SumPage;
                    this.PaddingSearch1.LinkSite = "tim-kiem-san-pham.htm?ZoneID=" + ZoneID + "&Search=" + Search + "&CompanyID=" + CompanyID + "&PageIndex";
                    this.PaddingSearch1.Padding(PageIndex);
                    PaddingSearch1.Padding(PageIndex);
                    //set meta
                    string Trang = "";
                    if (PageIndex > 1)
                    {
                        Trang = " | Trang " + PageIndex;
                    }
                    Page.Title = "Tìm kiếm Hồng Thủy" + Trang;
                    string hDesc = "Tìm kiếm Hồng Thủy" + Trang;
                    string hKey = "Tìm kiếm Hồng Thủy";
                    BAL.Utils.AddMeta(this.Page, BAL.Const.MetaAttributes.Description.Id, BAL.Const.MetaAttributes.Description.Name, hDesc);
                    BAL.Utils.AddMeta(this.Page, BAL.Const.MetaAttributes.Keywords.Id, BAL.Const.MetaAttributes.Keywords.Name, hKey);
                    BAL.Utils.AddMeta(this.Page, BAL.Const.MetaAttributes.NewKeywords.Id, BAL.Const.MetaAttributes.NewKeywords.Name, hKey);
                }
            }
        }
    }
}